﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using WdpaDownloadServiceRef;

public partial class Default2 : System.Web.UI.Page
{
    private string m_strAreTheyProteus;
    private string m_userID; 

    protected void Page_Load(object sender, EventArgs e)
    {

        if (User.Identity.IsAuthenticated)
        {
            string strEmail = User.Identity.Name;

            ProteusPartners M_PP = new ProteusPartners();

            if (M_PP.AreTheyProteusPartners(strEmail))
            {
                m_strAreTheyProteus = "true";
            }

            if (m_strAreTheyProteus == "true")
            {
                divProteusNo.Visible = false;
                paraRegUser.Visible = false;
                downloadSubmit.Visible = true;
            }


            //Read the T and C's from the text file and output them into a div
            // 
            string mapTandCFilePath = Server.MapPath("") + "/TandC.txt"; ;
            string readTandC = File.ReadAllText(mapTandCFilePath);

            scroll_box.InnerHtml = readTandC;
            divshowDownloads.Visible = true;
            divnotloggedin.Visible = false;

            String dbConnS = ConfigurationManager.ConnectionStrings["WDPAConnectionString1"].ConnectionString;
            System.Data.SqlClient.SqlConnection sqlConnAR = new System.Data.SqlClient.SqlConnection(dbConnS);


            SqlCommand getIdcmd = new SqlCommand();



            getIdcmd.CommandText = "SELECT DISTINCT * FROM [Contact] WHERE ([Email] = '" + User.Identity.Name + "' OR [UserID] = '" + User.Identity.Name + "')";
            getIdcmd.Connection = sqlConnAR;


            sqlConnAR.Open();


            SqlDataReader returnOb;
            returnOb = getIdcmd.ExecuteReader(CommandBehavior.CloseConnection);


            if (returnOb.Read())
            {
                m_userID = returnOb["ID"].ToString();
            }
            sqlConnAR.Close();

        }


    }

    protected void AgreetoTandC_CheckedChanged1(object sender, EventArgs e)
    {
        if (AgreetoTandC.Checked)
        {
            downloadSubmit.Visible = true;
        }
        else
        {
            downloadSubmit.Visible = false;
        }
    }



    protected void BtnDownloadShp_Click(object sender, EventArgs e)
    {

        WdpaDownloadService WdpaDownloadService1 = new WdpaDownloadService();

        WdpaDownloadService1.getAnnualDownload(m_userID, DataUse.SelectedItem.Text);


        //String dbConn = ConfigurationManager.ConnectionStrings["WDPAConnectionString1"].ConnectionString;


        //System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(dbConn);
        //sqlConn.Open();


        //SqlCommand InsertDownloadDetails = new SqlCommand("INSERT into dbo.AnnualRelease(UserID, Date, DataUse, RequestDate)" + "values (@UserID, @Date, @DataUse, @RequestDate)", sqlConn);
        //InsertDownloadDetails.Parameters.AddWithValue("@UserID", Convert.ToInt32(m_userID));
        //InsertDownloadDetails.Parameters.AddWithValue("@Date", DateTime.Now);
        //InsertDownloadDetails.Parameters.AddWithValue("@DataUse", DataUse.SelectedItem.Text);
        //InsertDownloadDetails.Parameters.AddWithValue("RequestDate", DateTime.Now);

        //InsertDownloadDetails.ExecuteReader(CommandBehavior.CloseConnection);
        //InsertDownloadDetails.Parameters.Clear();
        //InsertDownloadDetails.Dispose();

        //sqlConn.Close();


        Response.Redirect("AnnualRelDownloads.aspx");

    }
}

